import {HttpClient, HttpParams} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {Observable} from "rxjs/Observable";
import 'rxjs/add/operator/map';
import {Performance} from "../../../model/pages/performance/personal-performance";
import {MemberAchieve} from "../../../model/pages/performance/member-achieve";
import {AchieveDetail} from "../../../model/pages/performance/group-achieve";
import {PerformanceType} from "../../../model/enum/performance";

/*
  Generated class for the PerformanceProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class PerformanceProvider {

    constructor(public http: HttpClient) {
        // console.log('Hello PerformanceProvider Provider');
    }

    /**
     * 获取个人业绩详情
     * @param {string} id id
     * @param {string} startTime 开始时间
     * @param {string} endTime 结束时间
     * @param {PerformanceType} type 类型
     * @returns {Observable<Performance>}
     */
    getAchievement(id: string, startTime: string, endTime: string, type: PerformanceType): Observable<any> {
        if (id === undefined) {
            id = ''
        }

        if (type === PerformanceType.Person) {
            let params = new HttpParams();
            params = params.set('id', id);
            params = params.set('startTime', startTime);
            params = params.set('endTime', endTime);
            return this.http.get('/achieve/v1/personal_achieve', {
                params: params,
                responseType: 'text'
            }).map(res => {
                if (res) {
                    return JSON.parse(res.toString())
                } else {
                    return res;
                }
            })
        } else if (type === PerformanceType.Municipality) {
            let params = new HttpParams();
            params = params.set('month', startTime);
            return this.http.get<Array<AchieveDetail>>('/achieve/v1/direct_group', {params});
        }
        else {
            let params = new HttpParams();
            params = params.set('groupId', id);
            params = params.set('startTime', startTime);
            params = params.set('endTime', startTime);
            return this.http.get<AchieveDetail>('/achieve/v1/member_overview', {params});
        }


    }

    /**
     * 获取成员业绩总览列表
     * @param {string} id
     * @param {string} time
     * @returns {Observable<Array<AchieveDetail>>}
     */
    getPersonalAchieveList(id: string, time: string): Observable<Array<AchieveDetail>> {
        if (id === undefined) {
            id = ''
        }
        let params = new HttpParams();
        params = params.set('groupId', id);
        params = params.set('month', time);

        return this.http.get<Array<AchieveDetail>>('/achieve/v1/member_achieve', {params});

    }

    /**
     * 获取组业绩总览列表
     * @param {string} time
     * @param {PerformanceType} type 类型
     * @returns {Observable<MemberAchieve>}
     */
    getGroupAchieveList(time: string, type: PerformanceType): Observable<Array<AchieveDetail>> {

        let params = new HttpParams();
        params = params.set('month', time);
        if (type === PerformanceType.Municipality) {
            return this.http.get<Array<AchieveDetail>>('/achieve/v1/direct_group', {params});
        } else {
            return this.http.get<Array<AchieveDetail>>('/achieve/v1/indirect_group', {params});
        }
    }




}
